//
//  TVCounponHeaderView.swift
//  iPhone
//
//  Created by Toj on 12/1/22.
//

import UIKit

protocol TVCounponHeaderViewDelegate: NSObjectProtocol {
    func counponHeaderViewDidTapClose(_ view: TVCounponHeaderView)
    func counponHeaderViewDidTapSelectedNil(_ view: TVCounponHeaderView)
}

class TVCounponHeaderView: UIView {
    weak var delegate: TVCounponHeaderViewDelegate?
    
    private let height36: CGFloat = 36
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupMakeAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    @objc private func didTapClose() {
        delegate?.counponHeaderViewDidTapClose(self)
    }
    
    @objc private func didTapSelectedNil() {
        delegate?.counponHeaderViewDidTapSelectedNil(self)
    }
    
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.text = "me_coupon".localized().uppercased()
        view.font = 16.ui_boldFont
        view.textColor = "#333333".to_color
        return view
    }()
   
    private lazy var closeButton: UIButton = {
        let view = UIButton()
        view.setImage("ic_close_def_g".to_image, for: .normal)
        view.addTarget(self, action: #selector(didTapClose), for: .touchUpInside)
        return view
    }()
    
    private lazy var dontLabel: UILabel = {
        let view = UILabel()
        view.text = "membership_CouponList_NotUse".localized()
        view.font = UIFont.boldSystemFont(ofSize: 14.f_scale)
        view.textColor = "#666666".to_color
        return view
    }()
    
    private lazy var grayView: UIView = {
        let view = UIView()
        view.st_cornerRadius = height36 * 0.5
        view.backgroundColor = "#F6F6F6".to_color
        return view
    }()
    
    private(set) lazy var selectedButton: UIButton = {
        let view = UIButton()
        view.setImage("com_radio_gray_nor".to_image, for: .normal)
        view.setImage("com_radio_gray_sel".to_image, for: .selected)
        view.addTarget(self, action: #selector(didTapSelectedNil), for: .touchUpInside)
        return view
    }()
}

extension TVCounponHeaderView {
    
    private func setupMakeAddSubviews() {
        addSubview(grayView)
        addSubview(titleLabel)
        addSubview(closeButton)
        addSubview(dontLabel)
        addSubview(selectedButton)
    }
    
    private func setupMakeLayoutSubviews() {
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(16.ui_scale)
            make.top.equalTo(10.ui_scale)
        }
        
        closeButton.snp.makeConstraints { make in
            make.centerY.equalTo(titleLabel)
            make.height.equalTo(60)
            make.width.equalTo(60)
            make.right.equalTo(self)
        }
        
        dontLabel.snp.makeConstraints { make in
            make.bottom.equalTo(self).inset(17)
            make.left.equalTo(36)
        }
        
        selectedButton.snp.makeConstraints { make in
            make.centerY.equalTo(dontLabel)
            make.height.equalTo(height36)
            make.width.equalTo(48)
            make.right.equalTo(self).inset(16)
        }
        
        grayView.snp.makeConstraints { make in
            make.top.height.equalTo(selectedButton)
            make.left.equalTo(dontLabel.snp.left).offset(-20)
            make.right.equalTo(selectedButton)
        }
    }
}
